<?php

namespace App\Http\Controllers;
use App\Models\goods;
use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Requests\GoodsPostRequest;
use App\Http\Controllers\Controller;

class GoodsController extends Controller
{   
    //后台商品添加页
   public function getAdd()
   {	
   		//分类列表
   		// //查询
   		// $cates = DB::table('cate')->get();
   		$cates = DB::table('cate')
    		->select(DB::raw('*,concat(path,",",id) as paths'))
    		->orderBy('paths')
    		->get();
   		//遍历
		foreach ($cates as $k => $v) {
			//获取path路径的长度 
			$arr = explode(',',$v->path);
			//1为顶级类不用加|---- 2个长度为二级类加|---- 3个长度为三级类加两个|----
			$len = count($arr)-1;
			//给name加|----  所有改的是name的值$cates[$k]->name = ....
			$cates[$k]->cname = str_repeat('|----', $len).$v->cname ;	
		}
        //解析模板
        return view('admin.goods.add',['cates'=>$cates]);
   }

   //执行后台商品添加
   public function postInsert(GoodsPostRequest $request)
   {	
   		// dd($request->all());
   		//实例化模型对象
   		$goods = new Goods;
   		
   		//处理数据
   		$goods->cid = $request->input('cid');
   		$goods->title = $request->input('title');
   		$goods->price = $request->input('price');
   		$goods->kucun = $request->input('kucun');
   		$goods->content = $request->input('content');
   		$goods->status = $request->input('status');
   		
   		//处理上传图片
   		$goods->pic = UserController::upload($request);
   		
   		//执行数据插入
   		$res = $goods->save();
   		// 判断
        if($res)
        {
            return redirect('/admin/goods/index')->with('success','添加成功');
        }else
        {
            return back()->with('error','添加失败');
        }

   }

   	//后台商品列表页
   public function getIndex(Request $request)
   {
   		    	//获取分页条数
        $num = $request->input('num',10);

    	//在laravel中给的where条件 传了一个闭包$query代表当前数据库对象
        $goods = DB::table('goods')
        	->where(function($query) use($request){
            //判断是否有where条件
            if ($request->input('keywords')) {
                $query->where('title','like','%'.$request->input('keywords').'%');        
            }
        })->paginate($num);
 
		//遍历
		foreach ($goods as $k => $v) {
			//获取path路径的长度 
			$arr = explode(',',$v->pid);
			//1为顶级类不用加|---- 2个长度为二级类加|---- 3个长度为三级类加两个|----
			$len = count($arr)-1;
			//给name加|----  所有改的是name的值$cates[$k]->name = ....
			$goods[$k]->cname = str_repeat('|----', $len).$v->title ;
		}
		// dd($cates);

        //解析模板
   		return view('admin.goods.index',['goods'=>$goods],['request'=>$request->all()]);
   }

   //后台商品删除
   public function getDelete(Request $request)
   {
   		//获取id
   		$id = $request->input('id');
   		// dd($id);
   		//执行删除
   		$goods = Goods::find($id);
   		$res = $goods->delete();

   		// 判断
        if($res)
        {
            return redirect('/admin/goods/index')->with('success','删除成功');
        }else
        {
            return back()->with('error','删除失败');
        }

   }

   //后台商品修改
   public function getEdit(Request $request)
   {
   		//分类列表
   		// //查询
   		// $cates = DB::table('cate')->get();
   		$cates = DB::table('cate')
    		->select(DB::raw('*,concat(path,",",id) as paths'))
    		->orderBy('paths')
    		->get();
   		//遍历
		foreach ($cates as $k => $v) {
			//获取path路径的长度 
			$arr = explode(',',$v->path);
			//1为顶级类不用加|---- 2个长度为二级类加|---- 3个长度为三级类加两个|----
			$len = count($arr)-1;
			//给name加|----  所有改的是name的值$cates[$k]->name = ....
			$cates[$k]->cname = str_repeat('|----', $len).$v->cname ;	
		}
        
   		//查询
   		$goods = Goods::find($request->input('id'));
        
        //显示模板 分配变量
        return view('admin.goods.edit',['goods'=>$goods,'cates'=>$cates]);
   }

   //执行后台商品修改
   public function postDoedit(Request $request)
   {
   		// dd($request->all());
   		//实例化模型对象
   		// $goods = new Goods;
   		$goods = Goods::find($request->input('id'));
   		$id = $request->input('id');
   		//处理数据
   		$goods->cid = $request->input('cid');
   		$goods->title = $request->input('title');
   		$goods->price = $request->input('price');
   		$goods->kucun = $request->input('kucun');
   		$goods->content = $request->input('content');
   		$goods->status = $request->input('status');

   		// dd($goods);
   		 //处理图片上传
        if($request->hasFile('pic')){
            $goods['pic'] = UserController::upload($request);
        }else{
            $goods['pic'] = DB::table('goods')->where('id',$id)->first()->pic;
        }
        
        // dd($goods);
    	//执行数据修改
       	 $res = $goods->save();

        // dd($res);
    	// 判断
        if($res)
        {
            return redirect('/admin/goods/index')->with('success','修改成功');
        }else
        {
            return back()->with('error','修改失败');
        }
   }


}
